package com.lyc.servlet.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyc.dao.SportingEventsDAO;
import com.lyc.dao.imp.SportingEventsDAOImpl;
import com.lyc.entity.SportingEvents;

@WebServlet("/admin/SportingEvents-update.let")
public class SportingEventsUpdateServlet extends HttpServlet {

	private SportingEventsDAO dao = new SportingEventsDAOImpl();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
    	response.reset();
        response.setContentType("application/json;charset=UTF-8");
        
        try {
        	SportingEvents e = new SportingEvents();
        	String code = request.getParameter("seId");
        	if(code == null || "".equals(code)) {
        		throw new ServletException("项目编号不能为空");
        	}
        	if(!code.matches("^\\d+$")) {
        		throw new ServletException("项目编号必须是数字");
        	}
        	
            e.setSeId(Integer.parseInt(request.getParameter("seId")));
            e.setSeName(request.getParameter("seName"));
            e.setSeText(request.getParameter("seText"));
            try{
                dao.updateById(e);
                response.getWriter().print("{\"ok\":true}");
            }catch (Exception ex){
                throw new ServletException(ex.getMessage(), ex);
            }
        }catch(Exception e) {
        	response.getWriter().print("{\"ok\":false,\"msg\":\""+e.getMessage()+"\"}");
        }
        
        response.flushBuffer();
	}
}
